package com.volunteer.controller.admin;

import com.volunteer.common.R;
import com.volunteer.entity.Carousel;
import com.volunteer.service.CarouselService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/carousel")
@AllArgsConstructor
public class AdminCarouselController {

    private final CarouselService carouselService;

    /**
     * 获取所有轮播图
     * @return 轮播图列表
     */
    @GetMapping("/list")
    public R<List<Carousel>> list() {
        List<Carousel> carousels = carouselService.list();
        return R.ok(carousels);
    }

    /**
     * 新增轮播图
     * @param carousel 轮播图对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Carousel carousel) {
        boolean success = carouselService.save(carousel);
        return success ? R.ok("新增成功") : R.fail("新增失败");
    }

    /**
     * 更新轮播图
     * @param carousel 轮播图对象（含ID）
     * @return 操作结果
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody Carousel carousel) {
        boolean success = carouselService.updateById(carousel);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    /**
     * 删除轮播图
     * @param id 轮播图ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean success = carouselService.removeById(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * 根据ID查询轮播图详情
     * @param id 轮播图ID
     * @return 轮播图对象
     */
    @GetMapping("/get/{id}")
    public R<Carousel> getById(@PathVariable Long id) {
        Carousel carousel = carouselService.getById(id);
        return carousel != null ? R.ok(carousel) : R.fail("未找到该轮播图");
    }
}
